package com.fourth.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fourth.service.RoleService;
import com.fourth.vo.EmpConditionVO;
import com.github.pagehelper.PageInfo;
import com.ljx.entity.Permission;
import com.ljx.entity.Role;
import com.ljx.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author Mr.ma
 * @since 2023/2/27 12:03
 **/
@RestController
@RequestMapping("/emp/role")
@Api("角色操作接口")
public class ApiRoleController {
    @Resource
    private RoleService roleService;

    @RequestMapping("getRole")
    @ApiOperation("查询角色信息，含分页以及条件查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", value = "页码", dataType = "int" , defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示数量", dataType = "int" , defaultValue = "5"),
            @ApiImplicitParam(name = "condition", value = "查询条件", dataType = "Role")
    })
    public Result<IPage<Role>> selectAll(@RequestParam(defaultValue = "1") Integer currentPage,
                                               @RequestParam(defaultValue = "5")Integer pageSize,
                                               @RequestBody Role condition){
        IPage<Role> roles = roleService.selectAllByCondition(currentPage,pageSize,condition);
        return new Result<>(2000,"查询成功",roles);
    }

    @RequestMapping("addRole")
    @ApiOperation("插入角色信息")
    @ApiImplicitParam(name = "role", value = "角色信息", dataType = "Role")
    public Result insertRole(@RequestBody Role role){
        int row = roleService.insert(role);
        if (row > 0){
            return new Result(2000,"插入成功",null);
        }
        return new Result(5000,"插入失败",null);
    }

    @RequestMapping("updateRole")
    @ApiOperation("修改角色信息")
    @ApiImplicitParam(name = "role", value = "角色信息", dataType = "Role")
    public Result updateEmp(@RequestBody Role role){
        int row = roleService.updateRole(role);
        if (row > 0){
            return new Result(2000,"修改成功",null);
        }
        return new Result(5000,"修改失败",null);
    }

    @RequestMapping("deleteRole/{id}")
    @ApiOperation("删除角色信息")
    @ApiImplicitParam(name = "id", value = "角色编号", dataType = "String")
    public Result deleteById(@PathVariable String id){
        int row = roleService.deleteById(id);
        if (row > 0){
            return new Result(2000,"删除成功",null);
        }
        return new Result(5000,"删除失败",null);
    }

    @RequestMapping("selectRoleAll")
    @ApiOperation("获取所有角色信息")
    public Result selectRoleAll(){
        List<Role> roles = roleService.selectAll();
        return new Result<>(2000,"查询成功",roles);
    }

    @RequestMapping("upStatus/{id}/{status}")
    @ApiOperation("修改角色状态")
    public Result upStatus(@PathVariable("id") String id, @PathVariable("status")String status){

        int row = roleService.upStatus(id, status);
        if (row > 0){
            return new Result(2000,"修改状态成功",null);
        }
        return new Result(5000,"修改状态失败",null);
    }

    @RequestMapping("selectPermission/{roleId}")
    public Result<Map<String,List>> selectPermission(@PathVariable String roleId){
        Map<String,List> map = roleService.selectPermission(roleId);
        return new Result<Map<String,List>>(2000,"查询成功",map);
    }

    @RequestMapping("updatePermission/{roleId}")
    public Result updatePermission(@PathVariable String roleId, @RequestBody String[] menuids){
        System.out.println("roleId = " + roleId);
        System.out.println("menuids = " + menuids);
        int row = roleService.updatePermission(roleId,menuids);
        if (row > 0){
            return new Result(2000,"修改成功",null);
        }
        return new Result(5000,"修改失败",null);
    }

    @RequestMapping("queryPage")
    public Result<Map<String,String>> queryPage(@RequestParam(defaultValue = "1") Integer currentPage,
                                                @RequestParam(defaultValue = "5")Integer pageSize,
                                                @RequestBody Role condition){
        Map<String,String> roles = roleService.queryPage(currentPage,pageSize,condition);
        return new Result<>(2000,"查询成功",roles);
    }
}
